package com.zhicheng.controller;

import com.zhicheng.constant.JwtClaimsConstant;
import com.zhicheng.pojo.dto.UserDTO;
import com.zhicheng.pojo.result.PageResult;
import com.zhicheng.pojo.result.Result;
import com.zhicheng.pojo.User;
import com.zhicheng.pojo.dto.UserLoginDTO;
import com.zhicheng.pojo.vo.UserLoginVO;
import com.zhicheng.service.UserService;
import com.zhicheng.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Api(tags = "用户相关接口")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 注册用户
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/register")
    @ApiOperation("注册用户")
    public Result register(@RequestBody UserLoginDTO userLoginDTO) {
        userService.register(userLoginDTO);
        return Result.success();
    }

    /**
     * 用户登录
     *
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        //根据用户名查询用户
        User loginUser = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, loginUser.getId());
        claims.put("username", loginUser.getUsername());
        String token = JwtUtil.getToken(claims);
        //把token存储到redis中
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.set(token, token, 6, TimeUnit.HOURS);
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(loginUser.getId())
                .username(loginUser.getUsername())
                .nickname(loginUser.getNickname())
                .userPic(loginUser.getUserPic())
                .roleId(loginUser.getRoleId())
                .phone(loginUser.getPhone())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }

    @PutMapping("/update")
    @ApiOperation("用户个人信息修改")
    public Result update(@RequestBody UserDTO userDTO) {
        userService.update(userDTO);
        return Result.success();
    }

    @PatchMapping("/updatePwd")
    @ApiOperation("用户密码修改")
    public Result updatePwd(@RequestBody Map<String, String> params,@RequestHeader("Authorization") String token) {
        userService.updatePwd(params);
        // 删除redis中的token
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.getOperations().delete(token);
        return Result.success();
    }

    @PatchMapping("/updateAvatar")
    @ApiOperation("用户头像修改")
    public Result updateAvatar(@RequestParam String avatarUrl) {
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("分页查询用户信息")
    public Result<PageResult<User>> list(int page, int pageSize) {
        PageResult<User> pageResult = userService.list(page, pageSize);
        return Result.success(pageResult);
    }

    @PutMapping("/status/update")
    @ApiOperation("修改用户状态")
    private Result updateUserStatus(@RequestBody User user) {
        userService.updateUserStatus(user);
        return Result.success();
    }
}
